using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SEO.Model;
using SEO.DBSqlHelp;

namespace SEO.DAL
{

    /// <summary>
    /// 版权所有: 版权所有(C) 2011，上海云里网络科技有限公司
    /// 内容摘要: EDMMailsDAL
    /// 完成日期：2011年7月29日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class EDMMailsDAL
    {

        /// <summary>
        /// 获取EDMMails表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<EDMMails> GetAllEDMMailsDAL()
        {
            List<EDMMails> list = new List<EDMMails>();
            string strSql = "select * from EDMMails";
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    EDMMails es = new EDMMails();
                    es.MailId = int.Parse(dr["mail_id"].ToString());
                    es.MailEmail = dr["mail_email"].ToString();
                    es.MailUsername = dr["mail_username"].ToString();
                    es.MailTelphone = dr["mail_telphone"].ToString();
                    es.MailAddress = dr["mail_address"].ToString();
                    es.MailFax = dr["mail_fax"].ToString();
                    es.MailWebsite = dr["mail_website"].ToString();
                    es.UserId = int.Parse(dr["user_id"].ToString());
                    es.GroupID = int.Parse(dr["group_id"].ToString());
                    es.PostStatus = dr["post_status"].ToString();

                    list.Add(es);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMails -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取EDMMails表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<EDMMails> GetEDMMailsByWhereDAL(string strSql)
        {
            List<EDMMails> list = new List<EDMMails>();
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    EDMMails es = new EDMMails();
                    es.MailId = int.Parse(dr["mail_id"].ToString());
                    es.MailEmail = dr["mail_email"].ToString();
                    es.MailUsername = dr["mail_username"].ToString();
                    es.MailTelphone = dr["mail_telphone"].ToString();
                    es.MailAddress = dr["mail_address"].ToString();
                    es.MailFax = dr["mail_fax"].ToString();
                    es.MailWebsite = dr["mail_website"].ToString();
                    es.UserId = int.Parse(dr["user_id"].ToString());
                    es.GroupID = int.Parse(dr["group_id"].ToString());
                    es.PostStatus = dr["post_status"].ToString();

                    list.Add(es);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMails -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往EDMMails表中插入数据
        /// <param name="es">实例化EDMMails类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertEDMMailsDAL(SEO.Model.EDMMails es)
        {
            int iNum = 0;
            string strSql = "insert into EDMMails(mail_email,user_id,group_id) " +
             " values('" + es.MailEmail + "'," + es.UserId + "," + es.GroupID + ")";
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMails -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除EDMMails表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteEDMMailsDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMails -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新EDMMails表中数据
        /// </summary>
        /// <param name="es">实例化EDMMails类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateEDMMailsDAL(SEO.Model.EDMMails es)
        {
            int iNum = 0;
            string strSql = "update EDMMails set post_status='" + es.PostStatus + "' where mail_id=" + es.MailId;
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMails -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表EDMMails到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindEDMMailsDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "EDMMails");
                //ds.Tables["表名"].Columns.Add("字段名", typeof(string));
                for (int i = 0; i < ds.Tables["EDMMails"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["EDMMails"].Rows[i];
                    //dr["字段名"] = "字段值";
                }
                dgv.DataSource = ds.Tables["EDMMails"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMails -> " + ex.Message);
            }
        }

        /// <summary>
        /// 获取表中最大ID，并自增1
        /// </summary>
        /// <returns>返回表中最大ID，并自增1</returns>
        public int GetAutoAddNum()
        {
            int iNum = 0;
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                string strSql = "select top 1 mail_id from EDMMails order by mail_id";
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    iNum = int.Parse(dr["mail_id"].ToString()) + 1;
                }
                else
                {
                    iNum = 1;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定邮箱表 -> " + ex.Message);
            }

            return iNum;
        }

        /// <summary>
        /// 检测是否已存在数据库中，屏蔽已抓取的数据
        /// </summary>
        /// <param name="strEmail">电子邮件</param>
        /// <returns>返回 True or False</returns>
        public bool CheckMailIsExist(string strEmail,string strUserID,string strGroupID)
        {
            bool bValue = false;
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                string strSql = "select mail_id from EDMMails where mail_email='" + strEmail + "' and group_id=" + strGroupID + " and user_id=" + strUserID;
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    bValue = true;
                }
                else
                {
                    bValue = false;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("检测是否已存在数据库中 -> " + ex.Message);
            }

            return bValue;
        }


        /// <summary>
        /// 更新所有邮件为未发布状态
        /// </summary>
        /// <returns></returns>
        public int UpdateAllEDMMailsPostStatusBLL()
        {
            int iNum = 0;
            string strSql = "update EDMMails set post_status=0 ";
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新所有邮件为未发布状态 -> " + ex.Message);
            }
            return iNum;
        }

    }
}
